package com.baidumap.administrator.happystudy.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.ListUtils;
import com.ab.util.ToastUtil;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.TeacherMoreAdapter;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.TeacherIndex;
import com.baidumap.administrator.happystudy.view.SwipyRefreshLayout;
import com.socks.library.KLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by C on 2017/3/25.
 */
public class TeacherMoreActivity extends AppBaseActivity implements View.OnClickListener, OnMessageResponse, SwipyRefreshLayout.OnRefreshListener {

    private ListView mListview;
    private LinearLayout mTv1;
    private TextView mTv2;
    private TextView mTv3;
    private TextView mTv0;
    private PopupWindow mPopup;

    private List<TeacherIndex.DataBean> mdataBeenList = new ArrayList<>();
    private TeacherMoreAdapter teacherMoreAdapter;

    @Bind(R.id.main_SwipyRefreshLayout)
    SwipyRefreshLayout mSwipyRefreshLayout;

    private static int currentPage = 1;

    private String mOrderType = "rank";

    @Override
    public int bindLayout() {
        return R.layout.activity_teacher_more;
    }

    @Override
    public void initView(View view) {
        super.initView(view);

        mListview = (ListView) findViewById(R.id.listview);
        mTv1 = (LinearLayout) findViewById(R.id.tv1);
        mTv2 = (TextView) findViewById(R.id.tv2);
        mTv3 = (TextView) findViewById(R.id.tv3);
        mTv0 = (TextView) findViewById(R.id.tv0);
        mTv1.setOnClickListener(this);
        mTv2.setOnClickListener(this);
        mTv3.setOnClickListener(this);


        initPopup();


        teacherMoreAdapter = new TeacherMoreAdapter(this, mdataBeenList);
        mListview.setAdapter(teacherMoreAdapter);
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();

                bundle.putString("teacher_id", mdataBeenList.get(position).getTeacher_id());

                startBundleActivity(bundle, TeacherDetailActivity.class);
            }
        });
        teacherMoreAdapter.notifyDataSetChanged();

        // 设置分页第一页的索引 默认为0
        mSwipyRefreshLayout.setFirstIndex(currentPage);
        // 监听事件
        mSwipyRefreshLayout.setOnRefreshListener(this);
        // LoadImage初始化
        currentPage = 1;
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.pai_ming);
        Api.teacherIndex(this, mOrderType, currentPage + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv1:

                mTv0.setTextColor(0xFF2AC5BD);
                mTv2.setTextColor(0xff000000);
                mTv3.setTextColor(0xff000000);

                orderSort("rank");
                break;
            case R.id.tv2:
//                setBackgroundAlpha(this, 0.5f);
//                mPopup.showAsDropDown(mTv2);
                mTv0.setTextColor(0xff000000);
                mTv2.setTextColor(0xFF2AC5BD);
                mTv3.setTextColor(0xff000000);

                orderSort("questions");
                break;
            case R.id.tv3:


                mTv0.setTextColor(0xff000000);
                mTv2.setTextColor(0xff000000);
                mTv3.setTextColor(0xFF2AC5BD);

                orderSort("income");
                break;
            case R.id.rl1:
                mPopup.dismiss();
                break;
            case R.id.rl2:
                mPopup.dismiss();
                break;
            case R.id.rl3:
                mPopup.dismiss();
                break;

        }
    }

    private void orderSort(String type) {
        mOrderType = type;
        currentPage = 1;
        mdataBeenList.clear();
        teacherMoreAdapter.notifyDataSetChanged();
        LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
        Api.teacherIndex(this, mOrderType, currentPage + "");
    }

    public void initPopup() {
        View view = LayoutInflater.from(this).inflate(R.layout.popup_jieti, null);
        RelativeLayout rl1 = (RelativeLayout) view.findViewById(R.id.rl1);
        RelativeLayout rl2 = (RelativeLayout) view.findViewById(R.id.rl2);
        RelativeLayout rl3 = (RelativeLayout) view.findViewById(R.id.rl3);
        TextView tv1 = (TextView) view.findViewById(R.id.tv1);
        TextView tv2 = (TextView) view.findViewById(R.id.tv2);
        TextView tv3 = (TextView) view.findViewById(R.id.tv3);
        ImageView iv1 = (ImageView) view.findViewById(R.id.iv1);
        ImageView iv2 = (ImageView) view.findViewById(R.id.iv2);
        ImageView iv3 = (ImageView) view.findViewById(R.id.iv3);
        rl1.setOnClickListener(this);
        rl2.setOnClickListener(this);
        rl3.setOnClickListener(this);

        mPopup = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopup.setFocusable(true);
        mPopup.setTouchable(true);
        mPopup.setBackgroundDrawable(new ColorDrawable(Color.argb(123, 133, 234, 134)));
        mPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (TeacherMoreActivity.this != null) {
                    setBackgroundAlpha(TeacherMoreActivity.this, 1f);
                }
            }
        });
    }

    /**
     * 设置页面的透明度
     *
     * @param bgAlpha 1表示不透明
     */
    public void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        activity.getWindow().setAttributes(lp);
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.TEACHER_INDEX) && jo != null) {
            TeacherIndex teacherIndex = new TeacherIndex(jo.toString());

            if (teacherIndex != null) {

                if (teacherIndex.getCode().equals("200")) {

                    List<TeacherIndex.DataBean> dataBeanList = teacherIndex.getData();
                    for (int i = 0; i < dataBeanList.size(); i++) {
                        mdataBeenList.add(dataBeanList.get(i));
                    }

                    ListUtils.removeDuplicate(mdataBeenList);

                    currentPageAdd(mdataBeenList);

                    teacherMoreAdapter.notifyDataSetChanged();

                } else {
                    ToastUtil.showMessage(this, teacherIndex.getMsg());
                }
            }
            mSwipyRefreshLayout.setRefreshing(false);
        }
    }

    protected void currentPageAdd(List<TeacherIndex.DataBean> mListView) {
        KLog.e("========" + currentPage);
        if (mListView.size() % 20 == 0 && mListView.size() > 0) {
            currentPage++;
        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }

    /**
     * 下拉刷新
     *
     * @param index 当前分页索引
     */
    @Override
    public void onRefresh(int index) {
        currentPage = 1;

        mdataBeenList.clear();
        teacherMoreAdapter.notifyDataSetChanged();

        Api.teacherIndex(this, mOrderType, currentPage + "");
    }

    /**
     * 加载更多
     *
     * @param index 当前分页索引
     */
    @Override
    public void onLoad(int index) {
//        mSwipyRefreshLayout.setRefreshing(false);
//        Api.shopList(SupermarketActivity.this, currentPage, mCatrgoryId, mLatitude, mLongitude);
        Api.teacherIndex(this, mOrderType, currentPage + "");

    }
}
